<?php

/**
 * Returns Related tags data
 * for one specific tag
 * Related tags are the tags that appear
 * in questions that also include this tag
 * a tag param is required and should include
 * just one tag for which related tags will
 * be returned. A maximum of 100 related tags
 * will be returned along with the count
 * of questions in which these 2 tags appear together.
 *
 */

namespace StudyBuddy\Api\v1;

use StudyBuddy\Api\Api;

class Relatedtags extends Api {

    protected $aRequired = array('tag');
    protected $aData = array();

    protected function main() {
        $this->getData()
                ->setOutput();
    }

    protected function getData() {
        $tag = $this->oRequest->get('tag', 's', '');
        if (empty($tag)) {
            throw new \StudyBuddy\HttpResponseCodeException('Invalid value of "tag" param in request. It cannot be empty! Must be a name of the tag', 406);
        }

        $this->aData = $this->oRegistry->Mongo->RELATED_TAGS
                ->findOne(array('_id' => $tag), array('_id' => 1, 'tags' => 1));

        if (empty($this->aData)) {
            throw new \StudyBuddy\HttpResponseCodeException('No Data for tag ' . $tag, 404);
        }

        return $this;
    }

    /**
     *
     * Set to $this->oOutput object with
     * data from $this->aData
     *
     * @return object $this
     */
    protected function setOutput() {
        $this->oOutput->setData($this->aData);

        return $this;
    }

}